package com.hoso.android.bmicalc;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;



public class ResultActivity extends Activity{
	private int height = 0;
	private int weight = 0;
	private int bmi = 0;
	private NotificationManager notificationManager;
	
	@Override
	public void onCreate(Bundle icicle){
		super.onCreate(icicle);
		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		setContentView(R.layout.result);
		TextView bmiValue = (TextView)findViewById(R.id.label_bmivalue);
		TextView status = (TextView)findViewById(R.id.label_status);
		Bundle extras = getIntent().getExtras();
		if(extras != null){
			height = extras.getInt("HEIGHT");
			weight = extras.getInt("WEIGHT");
			bmi = 10000 * weight / height / height;
			bmiValue.setText(String.valueOf(bmi));
			if(bmi>24){
				status.setText(R.string.label_too_fat);
			}
			else if(bmi < 18){
				status.setText(R.string.label_too_slim);
			}
			else{
				status.setText(R.string.label_standard);
			}
		}
		Button saveResult = (Button) findViewById(R.id.button_save_result);
		Button close = (Button)findViewById(R.id.button_close_current_activity);
		saveResult.setOnClickListener(saveResultListener);
		close.setOnClickListener(closeListener);
		Button notify = (Button) findViewById(R.id.button_notify_result);
		notify.setOnClickListener(notifyListener);
	}
	
	private View.OnClickListener saveResultListener = new View.OnClickListener() {
		public void onClick(View view) {
			SharedPreferences preferences = getSharedPreferences("PREVIOUS_RESULT",MODE_PRIVATE);
			SharedPreferences.Editor editor = preferences.edit();
			editor.putInt("PREVIOUS_HEIGHT", height);
			editor.putInt("PREVIOUS_WEIGHT", weight);
			editor.commit();
			setResult(RESULT_OK);
			finish();
		}
	};
	
	private View.OnClickListener closeListener = new View.OnClickListener() {
		public void onClick(View view) {
			setResult(RESULT_CANCELED);
			finish();
		}
	};
	
	private View.OnClickListener notifyListener = new View.OnClickListener() {
		public void onClick(View view) {
			Intent intent = new Intent(Intent.ACTION_VIEW);
			Uri uri = Uri.parse("bmi:///result?measurements=" + height + "," +  weight + "," + bmi);
			intent.setData(uri);
			PendingIntent contentIntent = PendingIntent.getActivity(ResultActivity.this, 0, intent, 0);
			Notification notification = new Notification(android.R.drawable.btn_star
														, getString(R.string.ticker_message)
														, System.currentTimeMillis());
			notification.setLatestEventInfo(ResultActivity.this
											, getString(R.string.app_name)
											, getString(R.string.notification_description)
											, contentIntent);
			notification.flags = Notification.FLAG_AUTO_CANCEL;
			notificationManager.notify(R.string.app_name,notification);
		}
	};
}